package cn.ztf.zshop.sys.controller;

import cn.ztf.zshop.sys.vo.MenuVo;
import cn.ztf.zshop.sys.service.SysMenuService;
import cn.ztf.zshop.sys.dto.MenuDto;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 系统菜单管理
 */
@RestController
@RequestMapping("/sys/menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;


    @GetMapping("/menuTree")
    public ResponseEntity<List<MenuVo>> list() {
        List<MenuVo> list = sysMenuService.listTree();
        return ResponseEntity.ok(list);
    }

    @GetMapping("{id}")
    public ResponseEntity<MenuVo> get(@PathVariable Long id){
       MenuVo menuVo =  sysMenuService.getMenuById(id);
       return ResponseEntity.ok(menuVo);

    }

    @PostMapping
    public ResponseEntity<Void> add(@Validated @RequestBody MenuDto menuDto){
        sysMenuService.addMenu(menuDto);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    public ResponseEntity<Void> update(@Validated(MenuDto.Update.class) @RequestBody MenuDto menuDto){
        sysMenuService.updateMenu(menuDto);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleted(@PathVariable Long id){
        sysMenuService.deletedMenuVById(id);
        return ResponseEntity.ok().build();
    }

}
